<?php
class Mlogs {
	var $collection		= null;		
	var $cache			= null;
	var $clear			= null;
	/*
	------- table mlogs
	------- field
	* userid
	* type: {
			--- friend ----			
			--- give ----
			--- diary ----
				30: create
				31: news comment
			--- photo ----
				40: post news
				41: new comment
			--- full-house ----
				50: news blast
				51: news cmt
			--- offline ----
				60: post offline
				61: news cmt
				62: reg offline
				63: invite offline
				64: accept/deny invite offline
				65: accepted/denied offline
				66: host cancel offline
				67: user cancel
			--- news ---
				70:	post
				71: cmt
			--- place ---
				80: post
				81: cmt
				82: img
		}
	* content: json {posterid;....}
	* time
	*/
	public function __construct($dbName = DB_MONGO_VFRIEND) {
		if(DB_MONGO_ENABLE) {
			$mongo 				= new Mongo();
			$this->collection 	= $mongo->selectDB('vfriend')->selectCollection('mlogs');	
		}
	}
	public function delete() {
		if(DB_MONGO_ENABLE) {
			$this->collection->remove();
		}
	}	
	public function deleteById($intId) {
		if(DB_MONGO_ENABLE) {
			$this->collection->remove(array('_id' => new MongoId($intId)), true);
		}
	}	
	public function insert($intUserId, $intType, $strContent) {	
		try {
			if(DB_MONGO_ENABLE) {
				$intAllowInsert 		= 1;				
				$objVfriendUser 		= new Vfriend_User();
				$arrUserNotification	= $objVfriendUser->getUserNotification($intUserId);
				if($arrUserNotification) {
					switch(abs($intType)) {
						# diary
						case 30:
							$intAllowInsert = (($arrUserNotification['bemail']&(1<<0)) > 0) ? 1 : 0;
							break;
						case 31:
							$intAllowInsert = (($arrUserNotification['bemail']&(1<<1)) > 0) ? 1 : 0;
							break;
						# image
						case 40:						
							$intAllowInsert = (($arrUserNotification['iemail']&(1<<0)) > 0) ? 1 : 0;
							break;
						case 41:
							$intAllowInsert = (($arrUserNotification['iemail']&(1<<1)) > 0) ? 1 : 0;
							break;
						# full-house						
						case 50:
							$intAllowInsert = (($arrUserNotification['femail']&(1<<0)) > 0) ? 1 : 0;
							break;						
						case 51:
							$intAllowInsert = (($arrUserNotification['femail']&(1<<1)) > 0) ? 1 : 0;
							break;						
						# offline							
						case 60:						
							$intAllowInsert = (($arrUserNotification['oemail']&(1<<0)) > 0) ? 1 : 0;
							break;
						case 61:
							$intAllowInsert = (($arrUserNotification['oemail']&(1<<1)) > 0) ? 1 : 0;
							break;
						case 62:
							$intAllowInsert = (($arrUserNotification['oemail']&(1<<2)) > 0) ? 1 : 0;
							break;
						case 63:
							$intAllowInsert = (($arrUserNotification['oemail']&(1<<3)) > 0) ? 1 : 0;
							break;
						case 64:
							$intAllowInsert = (($arrUserNotification['oemail']&(1<<4)) > 0) ? 1 : 0;
							break;
						case 65:
							$intAllowInsert = (($arrUserNotification['oemail']&(1<<5)) > 0) ? 1 : 0;
							break;
						case 66:
						case 67:
							$intAllowInsert = (($arrUserNotification['oemail']&(1<<6)) > 0) ? 1 : 0;
							break;						
					}
				} else {
					$intAllowInsert = 0;
				}
				
				if($intAllowInsert == 1) {
					$objInsert = array(	'userid'	=> (int)$intUserId, 
										'type' 		=> $intType,
										'content'	=> (string)$strContent,
										'time'		=> time()
								);		
					$this->collection->insert($objInsert);					
				}
			}
		} catch(Exception $e) {
			Vfriend_Function::logDb('Mlogs::insert - ' .$e->getMessage());			
			return null;
		}
	}
	public function getMlog($intLimit=MLOGS_LIMIT) {
		try {			
			if(DB_MONGO_ENABLE) {
				$arrResult	= null;
				$objSort	= array('time'=>1);
				$arrData 	= $this->collection->find()->sort($objSort)->limit((int)$intLimit);
				
				if((object)$arrData) {
					foreach($arrData as $value) {
						$value['postdate'] 	= date('Y-m-d H:i:s',$value['time']);
						$value['content']	= Zend_Json::decode($value['content']);
						$arrResult[] 		= $value;
					}					
				}
				return isset($arrResult) ? $arrResult: null;
			}
			return null;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Mlogs::getMlog - ' .$e->getMessage());			
			return null;
		}
	}
}
?>